﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <locale.h>

static void list_directory(const char *dirname);

int main(int argc, char *argv[])
{
    /* Select default locale */
    setlocale(LC_ALL, "");

    /* For each directory in command line */
    int i = 1;
    while (i < argc) {
        list_directory(argv[i]);
        i++;
    }

    /* List current working directory if no arguments on command line */
    if (argc == 1)
        list_directory(".");

    return EXIT_SUCCESS;
}

/*
 * List files and directories within a directory.
 */
#if 1
static void list_directory(const char *dirname)
{
    /* Open directory stream */
    DIR *dir = opendir(dirname);
    if (!dir) {
        /* Could not open directory */
        fprintf(stderr,
            "Cannot open %s (%s)\n", dirname, strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* Print all files and directories within the directory */
    struct dirent *ent;
    while ((ent = readdir(dir)) != NULL) {
        switch (ent->d_type) {
        case DT_REG:
            printf("%s\n", ent->d_name);
            break;

        case DT_DIR:
            printf("%s/\n", ent->d_name);
            break;

        case DT_LNK:
            printf("%s@\n", ent->d_name);
            break;

        default:
            printf("%s*\n", ent->d_name);
        }
    }

    closedir(dir);
}
#endif

#if 0
static void list_directory(const char *dirname)
{
    char *buff;
    int i = 0;
    char file_name[64] = "";
    /* Open directory stream */
    DIR *dir = opendir(dirname);
    if (!dir) {
        /* Could not open directory */
        fprintf(stderr,
            "Cannot open %s (%s)\n", dirname, strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* Print all files and directories within the directory */
    struct dirent *ent;
    while ((ent = readdir(dir)) != NULL)
    {
        i++;
        printf("i == %d\n",i);
        //printf("----------1----------\n");
        //printf("%s\n", ent->d_name);
#if 0
        if(DT_REG == ent->d_type)
        {
            printf("%s\n", ent->d_name);
            //寻找数据包起始位置
            buff = strstr(ent->d_type, ".txt");
            if(NULL != buff)
            {
                printf("----------3----------\n");
                //strncpy(file_name,ent->d_name,(strlen(ent->d_name)-4));
                startup_file(ent->d_type);
                func1();
                close_file();
            }
            else
            {
                printf("----------2----------\n");
                continue;
            }
        }
#endif

#if 1
        switch (ent->d_type)
        {
            //标准文件
            case DT_REG:
                printf("DT_REG:%s\n", ent->d_name);
                //printf("%s\n", ent->d_name);
                //寻找数据包起始位置
                buff = strstr(ent->d_type, ".txt");
                if(NULL != buff)
                {
                    //printf("----------3----------\n");
                    //strncpy(file_name,ent->d_name,(strlen(ent->d_name)-4));
                    //startup_file(ent->d_type);
                    //func1();
                    //close_file();
                }
                else
                {
                    //printf("----------2----------\n");
                    continue;
                }

                break;
            //文件夹
            case DT_DIR:
                printf("DT_DIR:%s/\n", ent->d_name);
                break;
            //
            case DT_LNK:
                printf("DT_LNK:%s@\n", ent->d_name);
                break;

            //default:
                //printf("%s*\n", ent->d_name);
        }
#endif
    }

    closedir(dir);
}
#endif
